# -*- coding: utf-8 -*-

from sys import argv

import sys
import os
	
os.chdir("..")
path=os.getcwd()
sys.path += [path]
os.environ['DJANGO_SETTINGS_MODULE'] = 'categories.settings'
os.chdir("categories")

#print len(argv)

if len(argv) == 6 or len(argv) == 4: 
	 
	 pass
else:
	print "Usage: python editauthor.py add name surname weburl xmlurl or \n"\
	 	"\t python editauthor.py aremove name surname"
	sys.exit(-1)
	 
if argv[1] == "add":
	
	something=__import__("categories.categoriesdb.models")	
	namesurname=argv[2]+" "+argv[3]
	url=argv[4]
	clsname = "yazar"
	c=something.categoriesdb.models.__getattribute__(clsname)
	y=c(yazaradi=namesurname, yazarlinki=url)
	try:
		y.save()
		f=open("xml-resources-takip", "a+")
		strng="\n"+"--"+" "+"--"+" "+argv[5]+" "+argv[2]+"-"+argv[3]
		f.write(strng)
		f.close()
	except:
		pass
	
	

if argv[1] == "remove":
	
	namesurname=argv[2]+"-"+argv[3]
	
	something=__import__("categories.categoriesdb.models")
	clsname = "yazar"
	c=something.categoriesdb.models.__getattribute__(clsname)
	try:
		f=open("xml-resources-takip", "r+")
		s=f.readlines()

		counter,index=0,0
	
		for x in s:
			if x.find(namesurname) != -1: index=counter
			else: counter+=1
		
		#print index
		#print s[index]
		s.remove(s[index])
		try:
			s.remove("\n")
		except: pass
		f.close()
		os.remove("xml-resources-takip")
		f=open("xml-resources-takip", "w+")
		f.writelines(s)
		f.close()
		yzr=c.objects.get(yazaradi=argv[2]+" "+argv[3])
		yzr.delete()
		
	except:
		pass
	
	
	